﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace DFGK.Service
{
    public class MenuActionService : BaseService, IMenuActionService
    {
        private readonly IUnitOfWork _unitOfWork;
        private readonly IMenuActionRepository _menuActionRepository;
        public MenuActionService(IMapper mapper, IMenuActionRepository _menuActionRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            this._menuActionRepository = _menuActionRepository;
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 添加菜单操作关系
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public bool AddMenuAction(CMenuAction m)
        {
            var menuaction = _menuActionRepository.GetEntityWhereAll(x => x.MenuId == m.Mid);
            if (menuaction.Any())
                _menuActionRepository.DelEntityList(menuaction.ToList());
            foreach (var menu in m.Aids.Split(','))
            {
                MenuAction ma = new MenuAction();
                ma.Id = YitIdHelper.NextId();
                ma.MenuId = m.Mid;
                ma.ActionId = Convert.ToInt64(menu);
                _menuActionRepository.AddEntity(ma);
            }
            return _unitOfWork.SaveChangesEntity();
        }
    }
}
